<template>
  <div>
    <el-row>
      <el-button
        plain
        icon="el-icon-refresh"
        type="warning"
        size="mini"
        @click="openData()"
        >刷新</el-button
      >
      <el-button
        icon="el-icon-circle-plus-outline"
        plain
        type="primary"
        size="mini"
        @click="addNewAccount()"
        >新增</el-button
      >
      <el-button
        plain
        type="danger"
        @click="initWXAccount()"
        icon="el-icon-key"
        size="mini"
        >启动微信</el-button
      >
    </el-row>
    <el-dialog
      title="微信配置编辑"
      v-model="FShowEdit"
      :append-to-body="true"
      :close-on-click-modal="false"
      width="800"
      destroy-on-close
      :show-close="1 == 2"
    >
      <TFormWXAccountEdit
        :zContextParams="FEditParams"
        :zContextOnCall="editCallBack"
      ></TFormWXAccountEdit>
    </el-dialog>

    <div style="height: 15px"></div>
    <el-table :data="FAccountList" style="width: 100%; overflow-y: auto">
      <el-table-column prop="FAppID" label="微信AppID"> </el-table-column>
      <el-table-column prop="FAppSecret" label="微信密钥"> </el-table-column>
      <el-table-column prop="FMessageToken" label="消息Token">
      </el-table-column>
      <el-table-column prop="FMessageEncodingAESKey" label="消息密钥">
      </el-table-column>
      <!-- <el-table-column prop="FAccessToken" label="微信AccessToken">
      </el-table-column> -->
      <el-table-column prop="FAccessTokenLastTime" label="AccessToken时间">
      </el-table-column>
      <el-table-column prop="FJsApiLastTime" label="JSAPI时间">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="350">
        <template #default="scope">
          <el-button
            plain
            type="primary"
            size="mini"
            @click="editAccount(scope.row)"
            >编辑</el-button
          >
          <el-button
            plain
            type="primary"
            size="mini"
            @click="setMenu(scope.row)"
            >菜单设置</el-button
          >
          <el-button
            plain
            type="primary"
            size="mini"
            @click="onTemplateList(scope.row)"
            >模板消息设置</el-button
          >
          <el-button
            plain
            size="mini"
            type="danger"
            @click="delAccount(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts">
import { Options, Vue } from "vue-class-component";
import BaseForm from "../BaseForm";
import TFormWXAccountEdit from "@/views/weixin/AccountEdit.vue";
import { IMenuItem } from "@/store";
import WXAdminApi, { TWXAccount } from "@/baselib/module/weixin/WXAdminApi";

//配合着用
@Options({
  components: {
    TFormWXAccountEdit,
  },
  data() {},
  methods: {},
})
export default class TFormWXAccount extends BaseForm {
  private FIndexAccount: TWXAccount = new TWXAccount();
  private FEditParams: any = {};
  private FShowEdit: boolean = false;
  private FAccountList: TWXAccount[] = [];
  public created() {
    this.openData();
  }
  private async openData() {
    let lResult = await WXAdminApi.GetWXAccountList();
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    }
    this.FAccountList = lResult.resultData;
    this.$myMsgHelp.msgHintAutoClose("获取数据成功");
  }
  //增加一条配置
  private addNewAccount() {
    this.FIndexAccount = new TWXAccount();
    this.FEditParams = { wxAccount: this.FIndexAccount };
    this.FShowEdit = true;
  }
  //编辑一条配置
  private editAccount(qAccount: TWXAccount) {
    this.FIndexAccount = qAccount;
    this.FEditParams = { wxAccount: this.FIndexAccount };
    this.FShowEdit = true;
  }
  //弹出编辑框，关闭框时回调
  private editCallBack(action: string, qAccount: TWXAccount) {
    if ((action = "close")) {
      this.FShowEdit = false;
      this.openData();
    }
  }
  //删除一条配置
  private async delAccount(qAccount: TWXAccount) {
    let lResult = await WXAdminApi.DelWXAccountByID(qAccount.FAccountID);
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    }
    this.openData();
  }
  private async initWXAccount() {
    let lResult = await WXAdminApi.InitWXAccountList();
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    }
    setTimeout(() => {
      this.openData();
    }, 2000);
  }
  private setMenu(qAccount: TWXAccount) {
    let lTag: IMenuItem = {
      menuIndexName: "TFormWXMenuList-" + qAccount.FAppID,
      menuComponent: "TFormWXMenuList",
      menuTitle: "微信菜单",
      menuIcon: "",
      menuParams: { WXAppID: qAccount.FAppID }, //参数
      menuChildren: [],
    };
    this.$myEvenBus.addTag(lTag);
  }

  private onTemplateList(qAccount: TWXAccount) {
    let lTag: IMenuItem = {
      menuIndexName: "TFormTemplateList-" + qAccount.FAppID,
      menuComponent: "TFormTemplateList",
      menuTitle: "微信消息模板管理",
      menuIcon: "",
      menuParams: { WXAppID: qAccount.FAppID }, //参数
      menuChildren: [],
    };
    this.$myEvenBus.addTag(lTag);
  }
}
</script>

<style>
</style>